import {reqLogin,reqUserInfo,reqLoginOut} from '@/api'
const actions={
    // 登录请求
    async getLogin($store,data){
        let result= await reqLogin(data);
        console.log(result.data.data);
        if (result.data.code==200) {
            $store.commit('SETTOKEN',result.data.data)
            return 'ok'
        }else{
            return Promise.reject([new Error("faile")])
        }
    },
    //获取用户信息
    async getUserInfo($store){
        let result=await reqUserInfo();
        console.log(result.data)
        if (result.data.code==200) {
            $store.commit("USERINFO",result.data.data)
            return 'ok'
        }else{
            return Promise.reject([new Error("faile")])
        }
        
    },
    // 退出登录
    async getLoginOut($store){
        let result=await reqLoginOut();
        console.log(result);
        if (result.data.code==200) {
            $store.commit("GETLOGINOUT")
            return 'ok'
        }else{
            return Promise.reject([new Error("faile")])
        }
    }
};
const mutations={
    //登录
    SETTOKEN(state,info){
        state.token=info.token;
        console.log(info)
        localStorage.setItem("token",info.token)
    },
    //用户信息
    USERINFO(state,userInfo){
        state.userInfo=userInfo;
    },
    // 退出登录
    GETLOGINOUT(state){
        localStorage.removeItem("token");
        state.token="";
        state.userInfo={};
        console.log(localStorage.getItem("token"),state.token);
    }
};
const state={
    token:localStorage.getItem('token'),
    userInfo:{}
};
const getters={};
export default{
    actions,
    mutations,
    state,
    getters
}